package com.ymq.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ymq.reggie.common.R;
import com.ymq.reggie.entity.Category;
import com.ymq.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
   private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody Category category){

        categoryService.save(category);
        log.info("categroy分类消息：{}",category);
        return R.success("分类保存成功");
    }


    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){

        Page page1 = new Page(page,pageSize);

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        log.info("wrapper:{}",wrapper);

        categoryService.page(page1,wrapper);
        log.info("page1:{}",page1);
        log.info("wrapper:{}",wrapper);

        return R.success(page1);
    }

    /**
     * 根据ids删除分类
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R<String> delete( Long ids){
        log.info("id为：{}",ids);
      //  categoryService.removeById(ids);

        categoryService.remove(ids);

        return R.success("删除成功");
    }

    @PutMapping()
    public R<String> updateCatepory(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }

    @GetMapping("/list")
    public R<List<Category>> listR(Category category){
        log.info("菜品分类");

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByAsc(Category::getCreateTime);
        List<Category> list = categoryService.list(queryWrapper);
        log.info("彩屏分内：{}",list.get(0).toString());


        return R.success(list);
    }
}
